const Router = require('koa-router');
const router = new Router();
// 删除角色
const {db, Op, QueryTypes} = require("../models/db");
const tool = require("../tools/tool");
router.post('/category/saveQtsr', async ctx => {
    let formData = ctx.request.body.formData;
    // let roleId = ctx.request.body.roleId
    let tid = ctx.user.tenantId
    let mod = 'category'
    let pid = await db.models[mod].findOne({
        where: {
            tenantId: tid,
            typeNumber: 'subject',
            name: '其他业务收入'
        }
    })
    formData.name = tool.xe.trim(formData.name)
    let data = {
        tenantId: ctx.user.tenantId,
        pid: pid.id,
        name: formData.name,
        pinYin: tool.py(formData.name),
        typeNumber: 'subject',
        fullName: pid.fullName +'_'+formData.name,
        pidCode: pid.code,
        direct: pid.direct,
    }
    if(formData.id) {
        let current = await db.models[mod].findOne({
            where: {
                id: formData.id
            }
        })
        if(!current) return ctx.err('未找到数据')
        if(current.isInit == 1) {
            return ctx.err('不能修改默认设置')
        }
        data.uby = ctx.user.id
        data.utime = tool.getTime()
        let re = await db.models[mod].count({
            where: {
                tenantId: tid,
                pid: pid.id,
                name: formData.name,
                id: {
                    [Op.ne]: formData.id
                }
            }
        })
        if(re > 0) return ctx.err('已存在相同名称')
        //
        await db.models[mod].update(data, {
            where: {
                id: formData.id
            }
        })
        data.id = formData.id
        return ctx.suc('修改成功', data)
    }
    else {
        data.cby = ctx.user.id
        data.ctime = tool.getTime()
        let codModel = await db.models[mod].findOne({
            where: {
                tenantId: tid,
                pid: pid.id,
            },
            order: [['id', 'desc']]
        })
        if(codModel) {
            let cd = parseInt(codModel.code) + 1
            data.code = cd.toString()
        }
        else {
            data.code = pid.code + '01'
        }
        // 是否有重名
        let re = await db.models[mod].count({
            where: {
                tenantId: tid,
                pid: pid.id,
                name: formData.name
            }
        })
        if(re > 0) return ctx.err('已存在相同名称')
        let ret = await db.models[mod].create(data)
        return ctx.suc('新增成功', ret)
    }
})
router.post('/category/saveQtzc', async ctx => {
    let formData = ctx.request.body.formData;
    // let roleId = ctx.request.body.roleId
    let tid = ctx.user.tenantId
    let mod = 'category'
    let pid = await db.models[mod].findOne({
        where: {
            tenantId: tid,
            typeNumber: 'subject',
            name: '其他业务支出'
        }
    })
    formData.name = tool.xe.trim(formData.name)
    let data = {
        tenantId: ctx.user.tenantId,
        pid: pid.id,
        name: formData.name,
        pinYin: tool.py(formData.name),
        typeNumber: 'subject',
        pidCode: pid.code,
        fullName: pid.fullName +'_'+formData.name,
        direct: pid.direct,
    }
    if(formData.id) {
        let current = await db.models[mod].findOne({
            where: {
                id: formData.id
            }
        })
        if(!current) return ctx.err('未找到数据')
        if(current.isInit == 1) {
            return ctx.err('不能修改默认设置')
        }
        data.uby = ctx.user.id
        data.utime = tool.getTime()
        let re = await db.models[mod].count({
            where: {
                tenantId: tid,
                pid: pid.id,
                name: formData.name,
                id: {
                    [Op.ne]: formData.id
                }
            }
        })
        if(re > 0) return ctx.err('已存在相同名称')
        //
        await db.models[mod].update(data, {
            where: {
                id: formData.id
            }
        })
        data.id = formData.id
        return ctx.suc('修改成功', data)
    }
    else {
        data.cby = ctx.user.id
        data.ctime = tool.getTime()
        let codModel = await db.models[mod].findOne({
            where: {
                tenantId: tid,
                pid: pid.id,
            },
            order: [['id', 'desc']]
        })
        if(codModel) {
            let cd = parseInt(codModel.code) + 1
            data.code = cd.toString()
        }
        else {
            data.code = pid.code + '01'
        }
        // 是否有重名
        let re = await db.models[mod].count({
            where: {
                tenantId: tid,
                pid: pid.id,
                name: formData.name
            }
        })
        if(re > 0) return ctx.err('已存在相同名称')
        let ret = await db.models[mod].create(data)
        return ctx.suc('新增成功', ret)
    }
})
// 结算账户
router.post('/category/saveAccount', async ctx => {
    let formData = ctx.request.body.formData;
    // let roleId = ctx.request.body.roleId
    let tid = ctx.user.tenantId
    let mod = 'category'
    let pid = await db.models[mod].findOne({
        where: {
            tenantId: tid,
            typeNumber: 'subject',
            name: '银行存款'
        }
    })
    formData.name = tool.xe.trim(formData.name)
    let data = {
        tenantId: ctx.user.tenantId,
        pid: pid.id,
        name: formData.name,
        pinYin: tool.py(formData.name),
        typeNumber: 'subject',
        fullName: pid.fullName +'_'+formData.name,
        pidCode: pid.code,
        direct: pid.direct,
    }
    if(formData.id) {
        let current = await db.models[mod].findOne({
            where: {
                id: formData.id
            }
        })
        if(!current) return ctx.err('未找到数据')
        if(current.isInit == 1) {
            return ctx.err('不能修改默认设置')
        }
        data.uby = ctx.user.id
        data.utime = tool.getTime()
        let re = await db.models[mod].count({
            where: {
                tenantId: tid,
                pid: pid.id,
                name: formData.name,
                id: {
                    [Op.ne]: formData.id
                }
            }
        })
        if(re > 0) return ctx.err('已存在相同名称')
        //
        await db.models[mod].update(data, {
            where: {
                id: formData.id
            }
        })
        data.id = formData.id
        return ctx.suc('修改成功', data)
    }
    else {
        data.cby = ctx.user.id
        data.ctime = tool.getTime()
        let codModel = await db.models[mod].findOne({
            where: {
                tenantId: tid,
                pid: pid.id,
            },
            order: [['id', 'desc']]
        })
        if(codModel) {
            let cd = parseInt(codModel.code) + 1
            data.code = cd.toString()
        }
        else {
            data.code = pid.code + '01'
        }

        // 是否有重名
        let re = await db.models[mod].count({
            where: {
                tenantId: tid,
                pid: pid.id,
                name: formData.name
            }
        })
        console.log(data)
        if(re > 0) return ctx.err('已存在相同名称')
        let ret = await db.models[mod].create(data)
        return ctx.suc('新增成功', ret)
    }
})

// 删除其他收入
router.post('/category/delQtsr', async ctx => {
    let ids = ctx.request.body.ids
    let tid = ctx.user.tenantId
    if(ids.length > 0) {
        let c = await db.models['invoiceOther'].count({
            where: {
                tenantId: tid,
                subjectId: {
                    [Op.in]: ids
                }
            }
        })
        if(c > 0) return ctx.err('已使用类别，不能删除')
        let d = await db.models['cardInfo'].count({
            where: {
                tenantId: tid,
                subjectId: {
                    [Op.in]: ids
                }
            }
        })
        if(d > 0) return ctx.err('已使用类别，不能删除')

    }
    await db.models['category'].destroy({
        where: {
            tenantId: tid,
            id: {
                [Op.in]: ids
            }
        }
    })
    return ctx.suc('已删除 ' + ids.length)
})
router.post('/category/delAccount', async ctx => {
    let ids = ctx.request.body.ids
    let tid = ctx.user.tenantId
    return ctx.err('不能删除账户')
})
router.post('/category/getAccount', async ctx => {
    let tid = ctx.user.tenantId
    let sql = `
        SELECT * from (
        select id,code, name,sort from category where pid in (SELECT id from category where tenantId=${tid} and typeNumber='subject' and name = '库存现金')  and isDel=0 
        union all 
        select id,code, name,sort from category where pid in (SELECT id from category where tenantId=${tid} and typeNumber='subject' and name = '银行存款')  and isDel=0 
        ) a ORDER BY code
    `
    let list = await db.query(sql, {type: QueryTypes.SELECT})
    return ctx.suc('', list)
})

module.exports = router;